extends Area2D


@export var slime_speed : float = -50
@export var animator : AnimatedSprite2D
@export var slime_collision : CollisionShape2D

var is_dead : bool = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
	if not is_dead:
		position += Vector2(slime_speed, 0)*delta
		animator.play("idle")
	if position.x < -147.0: # 硬编码消灭出屏史莱姆
		queue_free()


func _on_body_entered(body: Node2D) -> void:
	if body is CharacterBody2D:
		print("遭到史莱姆攻击！！！")
		body.game_over()

# 子弹消灭史莱姆逻辑
func _on_area_entered(area: Area2D) -> void:
	if area.is_in_group("Bullet"):
		print("命中！⭐⭐YADAZE⭐⭐")
		
		slime_collision.queue_free() # 避免史莱姆爆炸伤到玩家
		animator.play("death")
		$EnemyDeath.play()
		is_dead = true
		area.queue_free() # 子弹消失
		get_tree().current_scene.score += 1 # 计分加一
	
		await get_tree().create_timer(0.5).timeout
		queue_free()
